#include "instance.h"

Instance::Instance(const string& name, ClassId* classId):
mName(name),
mClassId(classId)
{
    // NOOP
}

Instance::~Instance()
{
    // NOOP
}

bool Instance::setName(string name)
{
    mName = name;
    return true;
}

bool Instance::load(DataStream* ds)
{
    assert(ds);
    assert(mClassId);

    try {
        if(getClassId()->getExtension() == ".zob") {
            // Verify ClassId hash
            Uint32 hash;
            if (!(*ds >> hash))
                throw("Could not read ClassId hash");
            if (hash != mClassId->getHash())
                throw("Wrong ClassId hash");
        }
    }
    catch (const char* error) {
        cerr << "Instance::load(ds) : " << error << endl;
        return false;
    }

    return true;
}

bool Instance::save(DataStream* ds)
{
    assert(ds);
    assert(mClassId);

    try {
        if(getClassId()->getExtension() == ".zob") {
            // Write ClassId hash
            Uint32 hash = mClassId->getHash();
            if (!(*ds << hash))
                throw("Could not write ClassId hash");
        }
    }
    catch (const char* error) {
        cerr << "Instance::save(ds) : " << error << endl;
    }

    return true;
}
